$(function () {
    let shippingStatus = getQueryString("shippingStatus");
    let payStatus = getQueryString("payStatus");
    let orderStatus = getQueryString("orderStatus");
    let url = '../order/queryAll';
    if (shippingStatus) {
        url += '?shippingStatus=' + shippingStatus;
    }
    if (payStatus) {
        url += '?payStatus=' + payStatus;
    }
    if (orderStatus) {
        url += '?orderStatus=' + orderStatus;
    }
    $("#jqGrid").jqGrid({
        url: url,
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '订单号', name: 'orderNo', index: 'orderNo', width: 150},
            {
                label: '订单状态', name: 'orderStatus', index: 'order_status', width: 80, formatter: function (value) {
                if (value == '0') {
                    return '待处理';
                } else if (value == '1') {
                    return '待付款';
                } else if (value == '2') {
                    return '待发货';
                } else if (value == '3') {
                    return '已发货';
                } else if (value == '4') {
                    return '完成';
                } else if (value == '5') {
                    return '退款中';
                } else if (value == '6') {
                    return '已退款';
                } else if (value == '7') {
                    return '待收货';
                }
                return value;
            }
            },
            {
                label: '付款状态', name: 'payStatus', index: 'pay_status', width: 80,
                formatter: function (value) {
                    if (value == '0') {
                        return '待支付';
                    } else if (value == '1') {
                        return '已支付';
                    } else if (value == '2') {
                        return '支付失败';
                    }
                    return value;
                }
            },
            {label: '收货人', name: 'receiver', index: 'receiver', width: 80},
            // {label: '省', name: 'province', index: 'province', width: 20 },
            // {label: '地市', name: 'city', index: 'city', width: 20 },
            // {label: '区县', name: 'district', index: 'district', width: 20 },
             {label: '收货地址', name: 'address', index: 'address', width: 80},
             {label: '联系电话', name: 'mobile', index: 'mobile', width: 80},
            // {label: '补充说明', name: 'postscript', index: 'postscript', width: 80 },
            // {label: '快递公司Id', name: 'shippingId', index: 'shipping_id', width: 80 },
            {label: '快递公司', name: 'logisticsCompany', index: 'logisticsCompany', width: 120},
            {label: '快递单号', name: 'logisticsNo', index: 'logisticsNo', width: 150},
            // {label: '付款方', name: 'payName', index: 'pay_name', width: 80},
            // {label: '快递费用', name: 'shippingFee', index: 'shipping_fee', width: 80},
            {label: '实际支付金额', name: 'actualPrice', index: 'actual_price', width: 100},
            // {label: '', name: 'integral', index: 'integral', width: 80 },
            // {label: '', name: 'integralMoney', index: 'integral_money', width: 80 },
            {label: '订单总价', name: 'orderPrice', index: 'order_price', width: 80},
            {
                label: '下单时间', name: 'createTime', index: 'create_time', width: 130,
                formatter: function (value) {
                    return transDate(value);
                }
            },
            // {label: '确认时间', name: 'confirmTime', index: 'confirm_time', width: 80 },
            // {label: '付款时间', name: 'payTime', index: 'pay_time', width: 80},
            // {label: '配送费用', name: 'freightPrice', index: 'freight_price', width: 80},
            // {label: '使用的优惠券', name: 'couponId', index: 'coupon_id', width: 80 },
            // {label: '', name: 'parentId', index: 'parent_id', width: 80 },
            // {label: '优惠价格', name: 'couponPrice', index: 'coupon_price', width: 80}
            // {label: '', name: 'callbackStatus', index: 'callback_status', width: 80 }
            {
                label: '操作', width: 80, align: 'center', sortable: false, formatter: function (value, col, row) {
                return '<button class="ivu-btn ivu-btn-primary ivu-btn-circle ivu-btn-small" onclick="vm.lookDetail(' + row.id + ')"><i class="fa fa-info-circle"></i>&nbsp;详情</button>';
            }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        shrinkToFit:true,
        //forceFit:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.pageNum",
            total: "page.pages",
            records: "page.total"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            /*$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});*/
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x" : "scroll"});
        }
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        detail: false,
        title: null,
        order: {orderDetailList:[]},
        detailList:[],
        shippings: [],
        /*tableData: [{bookname: 'aaaaaa',salePrice:10,quantity:12},{bookname:'平凡的世界',salePrice:10,quantity:12}],*/
        tableData:[],
        tableColumns: [
            {title:'商品名称',key:'bookname'},
            {title:'售价',key:'salePrice'},
            {title:'数量',key:'quantity'}
        ],
        q: {
            orderNo: '',
            orderStatus: '',
            payStatus: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },

        confirm: function (event) {  //确认收货
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            confirm('确定收货？', function () {
                $.ajax({
                    type: "POST",
                    url: "../order/confirm",
                    contentType: "application/json",
                    data: JSON.stringify(id),
                    success: function (r) {
                        if (r.status == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        sendGoods:function () { //提交发送
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            $.ajax({
                type: "POST",
                url: '../order/sendGoods',
                contentType: "application/json",
                data: JSON.stringify(id),
                success: function (r) {
                    if (r.status === "0000") {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        update: function (event) {  //修改订单
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.detail = true;
            vm.title = "修改";
            $.get("../order/info/" + id, function (r) {
                vm.order = r.order;
                vm.getDetailList(r.order.id)
            });
        },
        getDetailList: function (oid) {
            $.get("../orderdetail/select/"+oid, function (r) {
                var str ;
                for(var i = 0,lengths = r.list.length; i< lengths ; i++ ){
                   str = {bookname:r.list[i].book.name,salePrice:r.list[i].salePrice,quantity:r.list[i].quantity};
                    vm.tableData.push(str);
                }
                console.log("vm.tableData:"+vm.tableData)

            });
        },
        saveOrUpdate: function (event) {
            /* var url = vm.books.id == null ? "../books/save" : "../books/update";*/
            var url = "../order/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.order),
                success: function (r) {
                    if (r.status === "0000") {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        saveOrUpdateDetail: function (event) {
            /* var url = vm.books.id == null ? "../books/save" : "../books/update";*/
            var url = "../order/saveOrUpdate";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.orderDetail),
                success: function (r) {
                    if (r.status === "0000") {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.detail = false;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'orderSn': vm.q.orderNo,
                    'orderStatus': vm.q.orderStatus,
                    'payStatus': vm.q.payStatus
                },
                page: page
            }).trigger("reloadGrid");
        },
        print: function () {
            window.print();
        },
        lookDetail: function (rowId) { //第三步：定义编辑操作
            vm.detail = true;
            vm.title = "详情";
            $.get("../order/info/" + rowId, function (r) {
                vm.order = r.order;
                vm.getDetailList(r.order.id)
            });
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        }
    },
    created: function () {
        let vue = this;
        //快递公司
        /*$.get("../shipping/queryAll", function (r) {
            vue.shippings = r.list;
        });*/
    }

});